﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainPageViewModel.cs" company="Catel development team">
//   Copyright (c) 2008 - 2012 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catwitter.ViewModels
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Threading.Tasks;
    using Catel.MVVM;
    using Catel.MVVM.Services;
    using Factories;
    using Models;
    using Providers;

    /// <summary>
    /// MainWindow view model.
    /// </summary>
    public class MainWindowViewModel : ViewModelBase
    {
        #region Variables
        private readonly List<ISocialMediaProvider> _enabledSocialMediaProviders = new List<ISocialMediaProvider>();
        private readonly ISocialMediaProviderFactory _socialMediaProviderFactory;
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is created to use an IoC container.
        /// </remarks>
        public MainWindowViewModel()
            : this(null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        /// <param name="socialMediaProviderFactory">The social media provider factory.</param>
        /// <remarks>
        /// This constructor is created to allow custom dependency injection.
        /// </remarks>
        public MainWindowViewModel(ISocialMediaProviderFactory socialMediaProviderFactory)
        {
            if (socialMediaProviderFactory == null)
            {
                socialMediaProviderFactory = GetService<ISocialMediaProviderFactory>();
            }

            _socialMediaProviderFactory = socialMediaProviderFactory;

            VisitCatel = new Command<object>(OnVisitCatelExecute);
            Refresh = new Command<object>(OnRefreshExecute);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title
        {
            get { return "Social Media Stream"; }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets items.
        /// </summary>
        public ObservableCollection<ISocialMediaEntry> SocialMediaEntries { get; set; }
        #endregion

        #region Commands

        #region Properties
        /// <summary>
        /// Gets the VisitCatel command.
        /// </summary>
        public Command<object> VisitCatel { get; private set; }

        /// <summary>
        /// Gets the Refresh command.
        /// </summary>
        public Command<object> Refresh { get; private set; }
        #endregion

        #region Methods
        /// <summary>
        /// Method to invoke when the VisitCatel command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void OnVisitCatelExecute(object parameter)
        {
            var processService = GetService<IProcessService>();
            processService.StartProcess("http://catel.codeplex.com");
        }

        /// <summary>
        /// Method to invoke when the Refresh command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void OnRefreshExecute(object parameter)
        {
            RefreshSocialMedia();
        }
        #endregion

        #endregion

        #region Methods
        /// <summary>
        /// Initializes the object by setting default values.
        /// </summary>	
        protected override void Initialize()
        {
            IEnumerable<ISocialMediaProvider> socialMediaProviders = _socialMediaProviderFactory.GetProviders();
            foreach (var socialMediaProvider in socialMediaProviders)
            {
                //var messageService = GetService<IMessageService>();
                //if (messageService.Show(string.Format("Do you want to support '{0}'?", socialMediaProvider.Name),
                //                        string.Format("{0} support", socialMediaProvider.Name), MessageButton.YesNo) == MessageResult.Yes)
                //{
                _enabledSocialMediaProviders.Add(socialMediaProvider);
                //}
            }

            RefreshSocialMedia();
        }

        /// <summary>
        /// Refreshes all the social media entries.
        /// </summary>
        private void RefreshSocialMedia()
        {
            var pleaseWaitService = GetService<IPleaseWaitService>();
            pleaseWaitService.Show("Retrieving updates from all providers");

            var task = new Task(() =>
            {
                var entries = new List<ISocialMediaEntry>();

                foreach (var socialMediaProvider in _enabledSocialMediaProviders)
                {
                    pleaseWaitService.UpdateStatus("Retrieving updates from " + socialMediaProvider.Name);
                    entries.AddRange(socialMediaProvider.GetLast10Updates());
                }

                var dispatcherService = GetService<IDispatcherService>();
                dispatcherService.BeginInvoke(() => SocialMediaEntries = new ObservableCollection<ISocialMediaEntry>(entries.OrderByDescending(entry => entry.Timestamp)));

                pleaseWaitService.Hide();
            });

            task.Start();
        }
        #endregion
    }
}